package com.biao.synchronized_test;

public class Test05_wait_notify {

    private Object objectLock = new Object();

    public static void main(String[] args) {
        Test05_wait_notify waitNotify = new Test05_wait_notify();
        waitNotify.test();
    }

    private void test() {
        new Thread(() -> {
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + ">1<");
                try {
                    // 让线程休眠阻塞：
                    // wait() 方法必须在synchronized 代码块里面才能调用，不然报错
                    objectLock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + ">2<");
            }
        }).start();

        try {
            Thread.sleep(3000);
            synchronized (objectLock) {
                // 唤醒线程：
                // notify() 方法必须在synchronized 代码块里面才能调用，不然报错
                objectLock.notify();
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
